﻿using UnityEngine;
using UnityEngine.Video;
using HKTools;
using QFramework;

/// <summary>
/// 视频播放测试脚本
/// </summary>
public class Test_VideoPlay : HKVideo_BaseController
{
    [SerializeField, Header("测试的视频Clip01：")] VideoClip videoClip01;
    [SerializeField, Header("测试的视频Clip02：")] VideoClip videoClip02;

    [SerializeField, Header("测试的视频URL：")] string videoURL;

    [SerializeField, Header("视频播放器面板：")] HK_VideoCtrl videoCtrl;
    [SerializeField, Header("简易视频播放器面板：")] HK_SimpleVideoCtrl simpleVideoCtrl;

    void Start()
    {
        videoCtrl.Hide();
        simpleVideoCtrl.Hide();

        this.RegisterEvent<Event_HKSimpleVideo_OnVideoCompleted>(args =>
        {
            HKDebug.Log($"视频 {args._videoClipName} 播放完成");
        }).UnRegisterWhenGameObjectDestroyed(this);
    }

    void OnGUI()
    {
        GUILayout.BeginArea(new Rect(20, 20, 240, Screen.height - 40));
        GUILayout.BeginVertical(GUI.skin.box);

        GUILayout.Label("VideoCtrl 测试", GUI.skin.window);
        if (GUILayout.Button("播放本地视频01", GUILayout.Height(30)))
        {
            videoCtrl.Show();
            simpleVideoCtrl.Hide();
            videoCtrl.PlayClipVideo(videoClip01);
        }

        if (GUILayout.Button("播放本地视频02", GUILayout.Height(30)))
        {
            videoCtrl.Show();
            simpleVideoCtrl.Hide();
            videoCtrl.PlayClipVideo(videoClip02);
        }

        if (GUILayout.Button("播放在线视频", GUILayout.Height(30)))
        {
            videoCtrl.Show();
            simpleVideoCtrl.Hide();
            videoCtrl.PlayURLVideo(videoURL);
        }

        GUILayout.Space(10);

        GUILayout.Label("SimpleVideoCtrl 测试", GUI.skin.window);
        if (GUILayout.Button("播放本地视频01 (循环)", GUILayout.Height(30)))
        {
            videoCtrl.Hide();
            simpleVideoCtrl.Show();
            simpleVideoCtrl.SetVolume(1f);
            simpleVideoCtrl.PlayClipVideo(videoClip01, true);
        }

        if (GUILayout.Button("播放本地视频02 (单次)", GUILayout.Height(30)))
        {
            videoCtrl.Hide();
            simpleVideoCtrl.Show();
            simpleVideoCtrl.SetVolume(0.5f);
            simpleVideoCtrl.PlayClipVideo(videoClip02);
        }

        if (GUILayout.Button("播放在线视频 (循环)", GUILayout.Height(30)))
        {
            videoCtrl.Hide();
            simpleVideoCtrl.Show();
            simpleVideoCtrl.SetVolume(1f);
            simpleVideoCtrl.PlayURLVideo(videoURL, true);
        }

        if (GUILayout.Button("播放StreamingAssets视频", GUILayout.Height(30)))
        {
            videoCtrl.Hide();
            simpleVideoCtrl.Show();
            simpleVideoCtrl.PlayStreamingAssetsVideo("Video_Test01.mp4", true);
        }

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("暂停/继续"))
        {
            simpleVideoCtrl.PauseVideo();
        }
        if (GUILayout.Button("停止"))
        {
            simpleVideoCtrl.StopVideo();
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("快进"))
        {
            simpleVideoCtrl.SetFastFowardOrRewind(0.1f);
        }
        if (GUILayout.Button("倒退"))
        {
            simpleVideoCtrl.SetFastFowardOrRewind(-0.1f);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("1倍速"))
        {
            simpleVideoCtrl.SetPlaybackSpeed(1f);
        }
        if (GUILayout.Button("2倍速"))
        {
            simpleVideoCtrl.SetPlaybackSpeed(2f);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("音量增大"))
        {
            simpleVideoCtrl.SetVolume(simpleVideoCtrl.CurrentVoluem + 0.1f);
        }
        if (GUILayout.Button("音量减小"))
        {
            simpleVideoCtrl.SetVolume(simpleVideoCtrl.CurrentVoluem - 0.1f);
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("静音"))
        {
            simpleVideoCtrl.SetMute(true);
        }
        if (GUILayout.Button("取消静音"))
        {
            simpleVideoCtrl.SetMute(false);
        }
        GUILayout.EndHorizontal();

        GUILayout.EndVertical();
        GUILayout.EndArea();
    }
}
